import Vue from 'vue';
import VueRouter from 'vue-router';
import { routes } from './routes';
Vue.use(VueRouter);

const router = new VueRouter({
  // mode: "history",
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
});

/**
 * @description 路由拦截 beforeEach
 * */
router.beforeEach(async (to, from, next) => {
  try {
    // 3.判断是访问登陆页，有 Token 就在当前页面，没有 Token 重置路由到登陆页
    // console.log('to', to.path);
    if (to.path.toLocaleLowerCase() === '/') {
      // console.log('去');
      // console.log('from',from);
      // if (localStorage.getItem('username') == null && localStorage.getItem('logout') == null || localStorage.getItem('username') == null && localStorage.getItem('logout') == true) {
      //   console.log('没有哦登录');
      //   // next({ path: '/', replace: true });
      //   router.push('/');
      // }

      return next();
    }

    // 5.判断是否有 Token，没有重定向到 login 页面
    // console.log('555', localStorage.getItem('logout'));
    if (localStorage.getItem('username') == null) {
      // console.log('没有哦登录');
      // next({ path: '/', replace: true });
      router.push('/');
    }

    // 正常访问页面
    next();
  } catch (error) {
    console.log(error);
  }
});

export default router;
